blob: 18fe75deb894bd5057b12909c962bcf2542df582 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
'use client'
import { useState, useEffect } from 'react'
import { Button } from '@/components/ui/button'
import { Label } from '@/components/ui/label'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Badge } from '@/components/ui/badge'
import { toast } from 'sonner'
import { deleteContractItem } from '../actions/contract-actions'
import { getContractItems } from '../actions/data-actions'
import { ContractSelector } from './contract-selector'
import { Trash2 } from 'lucide-react'
interface Contract {
id: number
contractNo: string
contractName: string
status: string
projectId: number
vendorId: number
projectCode: string | null
projectName: string | null
vendorName: string | null
vendorCode: string | null
}
interface ContractItem {
id: number
contractId: number
itemId: number
description: string | null
quantity: number
unitPrice: number | null
ProjectNo: string | null
itemCode: string | null
itemName: string | null
packageCode: string | null
unitOfMeasure: string | null
}
interface ContractItemsEditFormProps {
preselectedContractId?: number
}
export function ContractItemsEditForm({ preselectedContractId }: ContractItemsEditFormProps) {
const [loading, setLoading] = useState(false)
const [selectedContract, setSelectedContract] = useState<Contract | undefined>()
const [contractItems, setContractItems] = useState<ContractItem[]>([])
// 계약 선택 시 아이템들 로드
useEffect(() => {
if (selectedContract) {
loadContractItems(selectedContract.id)
}
}, [selectedContract])
const loadContractItems = async (contractId: number) => {
setLoading(true)
try {
const result = await getContractItems(contractId)
if (result.success) {
setContractItems(result.data)
} else {
toast.error(result.error)
}
} catch (error) {
toast.error('계약 아이템을 불러오는 중 오류가 발생했습니다.')
} finally {
setLoading(false)
}
}
const handleDelete = async (itemId: number) => {
if (!confirm('정말로 이 계약 아이템을 삭제하시겠습니까?')) {
return
}
setLoading(true)
try {
const result = await deleteContractItem(itemId)
if (result.success) {
toast.success(result.message)
// 아이템 목록 새로고침
if (selectedContract) {
await loadContractItems(selectedContract.id)
}
} else {
toast.error(result.error)
}
} catch (error) {
toast.error('계약 아이템 삭제 중 오류가 발생했습니다.')
} finally {
setLoading(false)
}
}
return (
<Card>
<CardHeader>
<CardTitle>계약 아이템 삭제</CardTitle>
<CardDescription>
기존 계약의 아이템들을 삭제할 수 있습니다.
</CardDescription>
</CardHeader>
<CardContent>
<div className="space-y-6">
{/* 계약 선택 */}
<div>
<Label>계약 선택 *</Label>
<ContractSelector
selectedContract={selectedContract}
onContractSelect={setSelectedContract}
disabled={loading}
preselectedContractId={preselectedContractId}
/>
</div>
{selectedContract && (
<div className="bg-muted/50 p-3 rounded-md">
<div className="text-sm font-medium">선택된 계약</div>
<div className="text-sm text-muted-foreground">
[{selectedContract.contractNo}] {selectedContract.contractName}
</div>
</div>
)}
{/* 계약 아이템 목록 */}
{contractItems.length > 0 && (
<div>
<Label>계약 아이템 목록 ({contractItems.length}개)</Label>
<div className="mt-2 space-y-3 max-h-96 overflow-y-auto border rounded-md p-3">
{contractItems.map(item => (
<div key={item.id} className="p-3 bg-white border rounded-md">
<div className="flex items-start justify-between">
<div className="flex-1">
<div className="flex items-center gap-2 mb-2">
<span className="font-medium">{item.itemName || `아이템 ${item.itemId}`}</span>
{item.itemCode && (
<Badge variant="outline" className="text-xs">
{item.itemCode}
</Badge>
)}
{item.unitOfMeasure && (
<Badge variant="secondary" className="text-xs">
{item.unitOfMeasure}
</Badge>
)}
</div>
{item.ProjectNo && (
<div className="text-xs text-muted-foreground mb-1">
프로젝트: {item.ProjectNo} | 패키지: {item.packageCode}
</div>
)}
{/* 보기 모드만 유지 */}
<div className="text-sm space-y-1">
<div>수량: {item.quantity} | 단가: {item.unitPrice || 0}</div>
{item.description && (
<div className="text-muted-foreground">설명: {item.description}</div>
)}
</div>
</div>
{/* 삭제 버튼만 유지 */}
<div className="flex gap-1 ml-2">
<Button
type="button"
variant="ghost"
size="sm"
onClick={() => handleDelete(item.id)}
disabled={loading}
className="text-red-600 hover:text-red-700"
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
</div>
</div>
))}
</div>
</div>
)}
{selectedContract && contractItems.length === 0 && !loading && (
<div className="text-center py-8 text-muted-foreground">
선택된 계약에 아이템이 없습니다.
</div>
)}
{loading && (
<div className="text-center py-8 text-muted-foreground">
로딩 중...
</div>
)}
</div>
</CardContent>
</Card>
)
}
|